home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / src / others.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-29  |  10.7 KB  |  525 lines

  1. /*
  2.  * $Id: others.c,v 1.1.1.1 2000/04/07 19:44:51 hfrieden Exp $
  3.  *
  4.  * $Date: 2000/04/07 19:44:51 $
  5.  * $Revision: 1.1.1.1 $
  6.  *
  7.  * (C) 1999 by Hyperion
  8.  * All rights reserved
  9.  *
  10.  * This file is part of the MiniGL library project
  11.  * See the file Licence.txt for more details
  12.  *
  13.  */
  14.  
  15. #include "sysinc.h"
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. static char rcsid[] = "$Id: others.c,v 1.1.1.1 2000/04/07 19:44:51 hfrieden Exp $";
  21.  
  22.  
  23. #ifndef __PPC__
  24. extern struct IntuitionBase *IntuitionBase;
  25. extern struct ExecBase *SysBase;
  26. #endif
  27.  
  28. void GLAlphaFunc(GLcontext context, GLenum func, GLclampf ref)
  29. {
  30.     ULONG w3dmode;
  31.     W3D_Float refvalue = (W3D_Float)ref;
  32.  
  33.     GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  34.  
  35.     switch(func)
  36.     {
  37.         case GL_NEVER:
  38.             w3dmode = W3D_A_NEVER;
  39.             break;
  40.         case GL_LESS:
  41.             w3dmode = W3D_A_LESS;
  42.             break;
  43.         case GL_EQUAL:
  44.             w3dmode = W3D_A_EQUAL;
  45.             break;
  46.         case GL_LEQUAL:
  47.             w3dmode = W3D_A_LEQUAL;
  48.             break;
  49.         case GL_GREATER:
  50.             w3dmode = W3D_A_GREATER;
  51.             break;
  52.         case GL_NOTEQUAL:
  53.             w3dmode = W3D_A_NOTEQUAL;
  54.             break;
  55.         case GL_GEQUAL:
  56.             w3dmode = W3D_A_GEQUAL;
  57.             break;
  58.         case GL_ALWAYS:
  59.             w3dmode = W3D_A_ALWAYS;
  60.             break;
  61.         default:
  62.             GLFlagError(context, 1, GL_INVALID_ENUM);
  63.             break;
  64.     }
  65.  
  66.     W3D_SetAlphaMode(context->w3dContext, w3dmode, &refvalue);
  67. }
  68.  
  69.  
  70. void GLDrawBuffer(GLcontext context, GLenum mode)
  71. {
  72. }
  73.  
  74. void GLPolygonMode(GLcontext context, GLenum face, GLenum mode)
  75. {
  76.    context->CurPolygonMode = mode ;
  77.    return ;
  78. }
  79.  
  80. void GLShadeModel(GLcontext context, GLenum mode)
  81. {
  82.     //LOG(2, glShadeModel, "%d", mode);
  83.  
  84.       context->CurShadeModel = mode ;
  85.  
  86. //avoid excessive state changes:
  87.  
  88.    if(context->CurShadeModel != context->ShadeModel)
  89.    {
  90.     context->ShadeModel = mode;
  91.  
  92.     if (mode == GL_FLAT)
  93.     {
  94.         W3D_SetState(context->w3dContext, W3D_GOURAUD, W3D_DISABLE);
  95.     }
  96.     else if (mode == GL_SMOOTH)
  97.     {
  98.         W3D_SetState(context->w3dContext, W3D_GOURAUD, W3D_ENABLE);
  99.     }
  100.    }
  101. }
  102.  
  103.  
  104.  
  105. #define BLS(X) case GL_##X: src=W3D_##X; break
  106. #define BLD(X) case GL_##X: dest=W3D_##X; break
  107.  
  108. void GLBlendFunc(GLcontext context, GLenum sfactor, GLenum dfactor)
  109. {
  110.     ULONG src, dest;
  111.  
  112.    if((context->CurBlendSrc == sfactor) && (context->CurBlendDst == dfactor))
  113.     return;
  114.  
  115.  
  116.     context->CurBlendSrc = sfactor ;
  117.     context->CurBlendDst = dfactor ;
  118.  
  119.     switch(sfactor)
  120.     {
  121.         BLS(ZERO);
  122.         BLS(ONE);
  123.         BLS(DST_COLOR);
  124.         BLS(ONE_MINUS_DST_COLOR);
  125.         BLS(SRC_ALPHA);
  126.         BLS(ONE_MINUS_SRC_ALPHA);
  127.         BLS(DST_ALPHA);
  128.         BLS(ONE_MINUS_DST_ALPHA);
  129.         BLS(SRC_ALPHA_SATURATE);
  130.     default:
  131.         GLFlagError(context, 1, GL_INVALID_ENUM);
  132.     }
  133.  
  134.     switch(dfactor)
  135.     {
  136.         BLD(ZERO);
  137.         BLD(ONE);
  138.         BLD(SRC_COLOR);
  139.         BLD(ONE_MINUS_SRC_COLOR);
  140.         BLD(SRC_ALPHA);
  141.         BLD(ONE_MINUS_SRC_ALPHA);
  142.         BLD(DST_ALPHA);
  143.         BLD(ONE_MINUS_DST_ALPHA);
  144.     }
  145.  
  146.     // Try to set the mode, if unavailable, switch to
  147.     // (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) which is supported
  148.     // by almost all Amiga supported graphics cards
  149.  
  150.     if (W3D_SetBlendMode(context->w3dContext, src, dest) == W3D_UNSUPPORTEDBLEND)
  151.     {
  152.         if (context->NoFallbackAlpha == GL_FALSE)
  153.         {
  154.             W3D_SetBlendMode(context->w3dContext, W3D_SRC_ALPHA, W3D_ONE_MINUS_SRC_ALPHA);
  155.             context->AlphaFellBack = GL_TRUE;
  156.         }
  157.         else
  158.         {
  159.             context->AlphaFellBack = GL_FALSE;
  160.         }
  161.     }
  162.     else
  163.     {
  164.         context->AlphaFellBack = GL_FALSE;
  165.     }
  166.  
  167.     context->SrcAlpha = sfactor;
  168.     context->DstAlpha = dfactor;
  169. }
  170.  
  171.  
  172. void GLHint(GLcontext context, GLenum target, GLenum mode)
  173. {
  174.     ULONG hint;
  175.     switch(mode)
  176.     {
  177.         case GL_FASTEST:    hint = W3D_H_FAST; break;
  178.         case GL_NICEST:     hint = W3D_H_NICE; break;
  179.         case GL_DONT_CARE:  hint = W3D_H_AVERAGE; break;
  180.         default:
  181.             GLFlagError(context, 1, GL_INVALID_ENUM);
  182.             break;
  183.     }
  184.  
  185.     switch(target)
  186.     {
  187.         case GL_FOG_HINT:
  188.             W3D_Hint(context->w3dContext, W3D_H_FOGGING, hint);
  189.             break;
  190.         case GL_PERSPECTIVE_CORRECTION_HINT:
  191.             W3D_Hint(context->w3dContext, W3D_H_PERSPECTIVE, hint);
  192.             break;
  193.         case MGL_W_ONE_HINT:
  194.             if (mode == GL_FASTEST) context->WOne_Hint = GL_TRUE;
  195.             else            context->WOne_Hint = GL_FALSE;
  196.             break;
  197.         case MGL_FIXPOINTTRANS_HINT:
  198.             if (mode == GL_FASTEST) context->FixpointTrans_Hint = GL_TRUE;
  199.             else            context->FixpointTrans_Hint = GL_FALSE;
  200.             break;
  201.         default:
  202.             GLFlagError(context, 1, GL_INVALID_ENUM);
  203.     }
  204. }
  205.  
  206.  
  207. void  GLGetBooleanv(GLcontext context, GLenum pname, GLboolean *params)
  208. {
  209.     switch( pname )
  210.    {
  211.       case GL_DEPTH_TEST:
  212.           *params = context->CurDepthTest ;
  213.          return ;
  214.  
  215.       case GL_DEPTH_WRITEMASK:
  216.           *params = context->CurWriteMask ;
  217.          return ;
  218.  
  219.       default:
  220.           *params = GL_FALSE ;
  221.          GLFlagError(context, 1, GL_INVALID_ENUM);
  222.     return ;
  223.    }
  224.  
  225. }
  226.  
  227.  
  228. void  GLGetIntegerv(GLcontext context, GLenum pname, GLint *params)
  229. {
  230.     switch( pname )
  231.    {
  232.       case GL_POLYGON_MODE:
  233.           *params = context->CurPolygonMode ;
  234.          return ;
  235.  
  236.       case GL_SHADE_MODEL:
  237.           *params = context->CurShadeModel ;
  238.          return ;
  239.  
  240.       case GL_BLEND_SRC:
  241.           *params = context->CurBlendSrc ;
  242.          return ;
  243.  
  244.       case GL_BLEND_DST:
  245.           *params = context->CurBlendDst ;
  246.          return ;
  247.  
  248.       case GL_MAX_TEXTURE_SIZE:
  249.           *params = 256 ;
  250.          return ;
  251.  
  252.       case GL_UNPACK_ROW_LENGTH:
  253.           *params = context->CurUnpackRowLength ;
  254.          return ;
  255.  
  256.       case GL_UNPACK_SKIP_PIXELS:
  257.           *params = context->CurUnpackSkipPixels ;
  258.          return ;
  259.  
  260.       case GL_UNPACK_SKIP_ROWS:
  261.           *params = context->CurUnpackSkipRows ;
  262.          return ;
  263.  
  264.     case GL_MAX_TEXTURE_UNITS_ARB:
  265.         *params = MAX_TEXUNIT;
  266.        return ;
  267.       default:
  268.           *params = 0 ;
  269.          GLFlagError(context, 1, GL_INVALID_ENUM);
  270.     return ;
  271.    }
  272.  
  273. }
  274.  
  275.  
  276. const GLubyte * GLGetString(GLcontext context, GLenum name)
  277. {
  278.     switch(name)
  279.     {
  280.         case GL_RENDERER:
  281.             if (context->w3dContext)
  282.             {
  283.                 switch(context->w3dContext->CurrentChip)
  284.                 {
  285.                     case W3D_CHIP_VIRGE:
  286.                         return "MiniGL/Warp3D S3 ViRGE (virge)";
  287.                     case W3D_CHIP_PERMEDIA2:
  288.                         return "MiniGL/Warp3D 3DLabs Permedia 2 (permedia)";
  289.                     case W3D_CHIP_VOODOO1:
  290.                         return "MiniGL/Warp3D 3DFX Voodoo 1 (voodoo)";
  291.                     case W3D_CHIP_AVENGER_BE:
  292.                         return "MiniGL/Warp3D 3DFX Voodoo 3 (avenger)";
  293.  
  294.                     case W3D_CHIP_UNKNOWN:
  295.                         return "MiniGL/Warp3D Unknown graphics chip";
  296.                     default:
  297.                         return "MiniGL/Warp3D";
  298.                 }
  299.             }
  300.             else
  301.             {
  302.                 return "MiniGL/Warp3D";
  303.             }
  304.  
  305.         case GL_VENDOR:     return "Hyperion";
  306.         case GL_VERSION:    return "1.1";
  307.         case GL_EXTENSIONS:
  308.             switch(context->w3dContext->CurrentChip)
  309.             {
  310.                 case W3D_CHIP_AVENGER_BE:
  311.                 case W3D_CHIP_PERMEDIA2:
  312.                 return "GL_MGL_ARB_multitexture GL_EXT_compiled_vertex_array GL_MGL_packed_pixels GL_EXT_color_table";
  313.             }
  314.             return "GL_MGL_packed_pixels GL_EXT_color_table";
  315.  
  316.         default:            return "Huh?";
  317.     }
  318. }
  319.  
  320. void GLGetFloatv(GLcontext context, GLenum pname, GLfloat *params)
  321. {
  322.     int i;
  323.  
  324.     switch(pname)
  325.     {
  326.         case GL_MODELVIEW_MATRIX:
  327.             for (i=0; i<16; i++)
  328.             {
  329.                 *params = context->ModelView[context->ModelViewNr].v[i];
  330.                 params++;
  331.             }
  332.             return;
  333.         case GL_PROJECTION_MATRIX:
  334.             for (i=0; i<16; i++)
  335.             {
  336.                 *params = context->Projection[context->ProjectionNr].v[i];
  337.                 params++;
  338.             }
  339.             return;
  340.  
  341.         default:
  342.             GLFlagError(context, 1, GL_INVALID_ENUM);
  343.             return;
  344.     }
  345. }
  346.  
  347. GLenum GLGetError(GLcontext context)
  348. {
  349.     GLenum ret = context->CurrentError;
  350.     context->CurrentError = GL_NO_ERROR;
  351.     return ret;
  352. }
  353.  
  354. GLboolean GLIsEnabled(GLcontext context, GLenum cap)
  355. {
  356.     switch(cap)
  357.     {
  358.        case GL_BLEND:
  359.          return( context->Blend_State ) ;
  360.  
  361.       case GL_DEPTH_TEST:
  362.          return( context->DepthTest_State ) ;
  363.  
  364.     case GL_TEXTURE_2D:
  365.            return (context->Texture2D_State[context->ActiveTexture]) ;
  366.  
  367.     default:
  368.     #ifndef __VBCC__
  369.         GLFlagError(context, 1, GL_INVALID_ENUM);
  370.     #else
  371.         return GL_FALSE;
  372.     #endif
  373.     }
  374. }
  375.  
  376.  
  377. void MGLSetZOffset(GLcontext context, GLfloat offset)
  378. {
  379.     context->ZOffset = offset;
  380. }
  381.  
  382. void MGLWriteShotPPM(GLcontext context, char *filename)
  383. {
  384.     GLubyte *pixelline;
  385.     FILE *f;
  386.     int i;
  387.     size_t bytes;
  388.  
  389.     pixelline = (GLubyte *)malloc(context->w3dWindow->Width*3);
  390.     if (!pixelline) return;
  391.  
  392.     f = fopen(filename, "wb");
  393.     if (!f)
  394.     {
  395.         free(pixelline);
  396.         return;
  397.     }
  398.  
  399.     // Write PPM header
  400.     fprintf(f, "P6\n%d %d\n255\n", context->w3dWindow->Width,
  401.     context->w3dWindow->Height);
  402.  
  403.     for (i=0; i<context->w3dWindow->Height; i++)
  404.     {
  405.         (void)ReadPixelArray(pixelline, 0, 0, context->w3dWindow->Width, context->w3dWindow->RPort,
  406.             0, (UWORD)i, context->w3dWindow->Width, 1, RECTFMT_RGB);
  407.         bytes = fwrite(pixelline, context->w3dWindow->Width*3, 1, f);
  408.     }
  409.  
  410.     fclose(f);
  411.     free(pixelline);
  412. }
  413.  
  414. // NYI:
  415. void GLReadPixels(GLcontext context, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels)
  416. {
  417.     GLubyte *pixelrect;
  418.     GLubyte *dest;
  419.     int i;
  420.  
  421.     pixelrect = (GLubyte *)malloc(context->w3dWindow->Width*context->w3dWindow->Height*3);
  422.     if (!pixelrect)
  423.         return;
  424.  
  425.     dest = pixelrect;
  426.  
  427.     for (i=0; i<context->w3dWindow->Height; i++)
  428.     {
  429.         (void)ReadPixelArray(dest, 0, 0, context->w3dWindow->Width, context->w3dWindow->RPort, 0, (UWORD)i, context->w3dWindow->Width, 1, RECTFMT_RGB);
  430.  
  431.         dest += context->w3dWindow->Width * 3;
  432.     }
  433.  
  434.     //convert and copy to *pixels ....
  435.  
  436.  
  437.     free(pixelrect);
  438. }
  439.  
  440. void MGLKeyFunc(GLcontext context, KeyHandlerFn k)
  441. {
  442.     context->KeyHandler = k;
  443. }
  444.  
  445. void MGLSpecialFunc(GLcontext context, SpecialHandlerFn s)
  446. {
  447.     context->SpecialHandler = s;
  448. }
  449.  
  450. void MGLMouseFunc(GLcontext context, MouseHandlerFn m)
  451. {
  452.     context->MouseHandler = m;
  453. }
  454.  
  455. void MGLIdleFunc(GLcontext context, IdleFn i)
  456. {
  457.     context->Idle = i;
  458. }
  459.  
  460. void MGLExit(GLcontext context)
  461. {
  462.     context->Running = GL_FALSE;
  463. }
  464.  
  465. void MGLMainLoop(GLcontext context)
  466. {
  467.     // FIXME: This should become 68k
  468.     struct IntuiMessage *imsg;
  469.     struct Window *window;
  470.     ULONG Class;
  471.     UWORD Code;
  472.     WORD MouseX, MouseY;
  473.     GLbitfield buttons = 0;
  474.  
  475.     window = (struct Window *)mglGetWindowHandle();
  476.     ModifyIDCMP(window, IDCMP_VANILLAKEY|IDCMP_RAWKEY|IDCMP_MOUSEMOVE|IDCMP_MOUSEBUTTONS);
  477.  
  478.     context->Running = GL_TRUE;
  479.  
  480.     while (context->Running == GL_TRUE)
  481.     {
  482.         while (imsg = (struct IntuiMessage *)GetMsg(window->UserPort))
  483.         {
  484.             Class  = imsg->Class;
  485.             Code   = imsg->Code;
  486.             MouseX = imsg->MouseX;
  487.             MouseY = imsg->MouseY;
  488.             ReplyMsg((struct Message *)imsg);
  489.             switch(Class)
  490.             {
  491.             case IDCMP_VANILLAKEY:
  492.                 if (context->KeyHandler)
  493.                 {
  494.                     context->KeyHandler((char)Code);
  495.                 }
  496.                 break;
  497.             case IDCMP_MOUSEBUTTONS:
  498.                 switch(Code)
  499.                 {
  500.                     case SELECTDOWN: buttons |= MGL_BUTTON_LEFT;    break;
  501.                     case SELECTUP:   buttons &= ~MGL_BUTTON_LEFT;   break;
  502.                     case MENUDOWN:   buttons |= MGL_BUTTON_RIGHT;   break;
  503.                     case MENUUP:     buttons &= ~MGL_BUTTON_RIGHT;  break;
  504.                     case MIDDLEDOWN: buttons |= MGL_BUTTON_MID;     break;
  505.                     case MIDDLEUP:   buttons &= MGL_BUTTON_MID;     break;
  506.                 }
  507.             // drop through
  508.             case IDCMP_MOUSEMOVE:
  509.                 if (context->MouseHandler)
  510.                 {
  511.                     context->MouseHandler((GLint)MouseX, (GLint)MouseY, buttons);
  512.                 }
  513.                 break;
  514.             } /* switch Class */
  515.         } /* While imsg */
  516.  
  517.         if (context->Idle)
  518.         {
  519.             context->Idle();
  520.         }
  521.     } /* While running */
  522. }
  523.  
  524.  
  525.